$(function() {
	$(".btnAst").button();

	/**
	 * 展开
	 */
	var $btnShow = $("#btnShow");
	var $pnlExpand = $("#pnlExpand");

	var setBtnShow = function(flag) {
		if (!flag) {
			$btnShow.button({
						label : "展开",
						icons : {
							primary : "ui-icon-arrowthickstop-1-s",
							secondary : "ui-icon-arrowthickstop-1-s"
						}
					});
		} else {
			$btnShow.button({
						"label" : "关闭",
						icons : {
							primary : "ui-icon-arrowthickstop-1-n",
							secondary : "ui-icon-arrowthickstop-1-n"
						}
					});
		}
	};

	setBtnShow();
	$btnShow.toggle(function() {
				$pnlExpand.show();
				setBtnShow(true);
			}, function() {
				$("#pnlExpand").hide();
				setBtnShow(false);
			});

	// 出厂日期、购置日期、保修截至日期
	$("#buyDate,#delyDate,#storeDate").click(function(){
	 WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false,readOnly:true});
	});

	$("#collarName").blur(function() {
				if ($("#handName").val() == "") {
					$("#handName").attr("value", $("#collarName").val());
				}

			});

	// 单位树
	$("#deptName").deptTree({
				height : 300
			});
	// 国标树
	$("#gbCode").GBTree({
				height : 300,
				nodeEvent : function(value) {

					var data = value.split("-");
					$("#txtAstName").val(data[1]);
					$("#txtJlUnit").val(data[4]);
					$("#txtUseYear").val(data[3]);
                    $("#astClass").val(data[2]);
				}
			});
	// 国别
	$("#nationCode").nationList({
				height : 250,
				nodeEvent : function(value) {
					var data = value.split("-");
					$("#txtGBNo").val(data[0]);
					$("#txtGBName").val(data[1]);
				}
			});
	// 存放地点
	$("#txtStorage").ssList({
				height : 250,
				nodeEvent : function(value) {
					var data = value.split("-");
					$("#txtCfddNo").val(data[0]);
					$("#txtCfddName").val(data[1]);
				}
			});

	// 图片控制
	var picCount = 0;
	var $picList = $("#picList");
	var $tdPic = $("#tdPic");

	if ($picList.find("dl").size() < 5) {
		$("#addFile").click(function() {
			if (picCount < 4 - $picList.find("dl").size()) {

				$tdPic
						.append('<span class="pic-box" ><input type="file" class="ui-widget-content ui-corner-all txt" name="pic"><a name="btnDelPicBox" href="#">删除</a></span>');

				picCount++;
			}

			return false;
		});
	}

	$("a[name='btnDelPicBox']").live("click", function() {
				picCount--;
				$(this).parent().remove();
				return false;
			});

	var $oldPic = $("#oldPicUrl");
	var $delPic = $("#delPicUrl");
	var delURL = "";
	var $t = "", temp = "";
	var getPicURLs = function() {
		var url = "";
		$picList.find("img").each(function() {
					url += "," + $(this).attr("src").replace(/^\/swss/, "");
				});
		return url.substring(1);
	};
	$picList.find("dd>a").click(function() {

		$t = $(this).parent().parent();

		
		temp = $delPic.val() + ","
				+ $t.find("img").attr("src").replace(/^\/swss/, "");

		$delPic.val(temp.replace(/^,/,""));
		
		$t.remove();

		$oldPic.val(getPicURLs());

		return false;
	});

	// 单价格式化
	var $txtPrice = $("#txtPrice");
	$txtPrice.blur(function() {
				$("#txtPrice").val($.formatCurrency($txtPrice.val()));
			});

	/**
	 * 提交表单过程函数
	 */

	var submitForm = function(form) {

		swss.ui.loading('show', '保存中...');
		$(form).ajaxSubmit({
					type : 'post',
					dataType : 'json',
					success : function(res) {
						swss.ui.loading('hide');
						$("#dialogGoon").centerShow();
						$("#mask").mask();
						return false;
					},
					error : function(a, b, c) {
						alert("表单提交错误:" + a + "==" + b + "==" + c);
					}

				});

	};
	// 验证
	$.validator.setDefaults({
				submitHandler : function(form) {
					// submitForm(form);
					 swss.ui.confirm(" 您确定修改编号 "+$("#astNo").val()+" 的资产？", function() {
					 form.submit();
					});
				},
				highlight : function(input) {
					$(input).addClass("ui-state-highlight");
				},
				unhighlight : function(input) {
					$(input).removeClass("ui-state-highlight");
				}
			});

	$.validator.addMethod("quantity", function(value, element) {
				return this.optional(element)
						|| (parseFloat(value.replace(/,/g, "")) > 0);
			}, "单价必须大于0");
	$.validator.addMethod("phonecheck", function(value, element) {
		var tel = /(^[0-9]{3,4}\-[0-9]{7,8}$)|(^[0-9]{7,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)|(13\d{9}$)|(15[0135-9]\d{8}$)|(18[267]\d{8}$)/;
		if (value != null) {
			return tel.exec(value);
		}
	}, "不合法的号码格式");
	
	$.validator.addMethod("finDeptNameCheck", function(value,element) {
				return value == 0 ? false : true
			}, "选择核销科室");
    $.validator.addMethod("clbzCheck", function(value, element) {
				return (value === 0 || value==="") ? false : true;
			}, "选择车辆编制");
	$("#astForm").validate({
				rules : {
					'asset.astName' : {
						required : true,
						maxlength:20
					},
					'gbCode' : {
						required : true
					},
					'asset.handName' : {
						required : true,
						maxlength:20
					},
					'asset.collarName':{
					    required : true,
					    maxlength:20
					},
					'asset.buyDate' : {
						required : true
					},
					'asset.astCount' : {
						required : true,
						digits : true
					},
					'asset.price' : {
						required : true,
						number : true,
						quantity : true
					},
					'asset.brand':{
					    required:true,
					    maxlength:20
					},
					'asset.modelNo':{
					    required:true,
					    maxlength:20
					},
					'deptName' : {
						required : true
					},
					'txtStorageread' : {
						required : true
					},
					'asset.telephoneNo':{
					   required : true,
					   phonecheck:true,
					},
					 'asset.finDeptName':{
				     finDeptNameCheck:true,
					 },
					 //车辆内容
					 'asset.frameNo':{
					   required : true,
					   maxlength : 30
					},
					'asset.engineNo':{
					   required : true,
					   maxlength : 30
					},
					'asset.cardNo':{
					   required : true,
					   maxlength : 30
					},
					'asset.clbz':{
					  clbzCheck:true
					}
				}
			});

	// ==
	DG.setCheckBox();

});

/**
 * 查询相关函数包
 */
var DG = {
	// 设置全选 复选框
	setCheckBox : function() {
		var $chkItems = $("input[name='chkItem']");
		$("#chkAll").click(function() {
					if ($(this).attr("checked")) {
						$chkItems.attr("checked", true);
					} else {
						$chkItems.attr("checked", false);
					}
				});
	}
};